package test2.sheets;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;

import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;

import test2.pojo.Se;
import test2.pojo.SeSubject;
import utils.WishColumn;
import utils.WishExcel;
import utils.WishModule;
import utils.handlers.RowBoldWriteHandler;

/**
 * 多个sheet，多个table
 * @author maml
 */
public class TeacherStudentTest
{
	public static void main(String[] args)
	{
		WishModule<Se> student = new WishModule<>("学生");
		student.setWriteHandlers(Arrays.asList(new RowBoldWriteHandler(1),new LoopMergeStrategy(2, 0)));
		{
			List<WishColumn<?>> headers = new ArrayList<>();
			WishColumn<Se> header1 = new WishColumn<>("姓名", x -> x.getName());
			WishColumn<Se> header2 = new WishColumn<>("年龄", x -> {
				if (x.getAge() > 18)
				{
					WriteCellData<Integer> writeCellStyle = new WriteCellData<>(new BigDecimal(x.getAge()));
					writeCellStyle.setType(CellDataTypeEnum.NUMBER);
					WriteCellStyle writeCellStyleData = new WriteCellStyle();
					writeCellStyle.setWriteCellStyle(writeCellStyleData);
					writeCellStyleData.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
					writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex());
					return writeCellStyle;
				}
				return x.getAge();
			});
			headers.add(header1);
			headers.add(header2);
			student.setTableHeaders(headers);
			student.setDatasSupplier(() -> {
				List<Se> datas = new ArrayList<>();
				Se a = new Se();
				a.setAge(18);
				a.setName("maml");
				datas.add(a);

				Se b = new Se();
				b.setAge(19);
				b.setName("wangmiaowangmiaowangmiaowangmiaowangmiao");
				datas.add(b);
				return datas;
			});
		}
		WishModule<Se> student1 = new WishModule<>("学生");
		student1.setWriteHandlers(Arrays.asList(new RowBoldWriteHandler(1),new LoopMergeStrategy(2, 0)));
		{
			List<WishColumn<?>> headers = new ArrayList<>();
			WishColumn<Se> header1 = new WishColumn<>("姓名", x -> x.getName());
			WishColumn<Se> header2 = new WishColumn<>("年龄", x -> "xxxx");
			headers.add(header1);
			headers.add(header2);
			student1.setTableHeaders(headers);
			student1.setDatasSupplier(() -> {
				List<Se> datas = new ArrayList<>();
				Se a = new Se();
				a.setAge(18);
				a.setName("A");
				datas.add(a);

				Se b = new Se();
				b.setAge(19);
				b.setName("B");
				datas.add(b);
				return datas;
			});
		}
		
		// teacher sheet
		WishModule<SeSubject> teacher = new WishModule<>("教师");
		{
			List<WishColumn<?>> headers = new ArrayList<>();
			WishColumn<SeSubject> header1 = new WishColumn<>("姓名", x -> x.getId());
			WishColumn<SeSubject> header2 = new WishColumn<>("年龄", x -> x.getName());

			headers.add(header1);
			headers.add(header2);
			teacher.setTableHeaders(headers);
			teacher.setDatasSupplier(() -> {
				List<SeSubject> datas = new ArrayList<>();
				SeSubject a = new SeSubject();
				a.setId(18l);
				a.setName("aaa");
				datas.add(a);

				SeSubject b = new SeSubject();
				b.setId(19);
				b.setName("aaaaaaaa");
				datas.add(b);
				return datas;
			});
		}

		try(ExcelWriter ew=WishExcel.write("test.xlsx", Arrays.asList(student, student1, teacher),false)){
			WriteSheet writeSheet = EasyExcel.writerSheet("教师").build();
			List<String> list = new ArrayList<>();
			list.add("123");
			list.add("456");
			Supplier<Collection<?>> supplier = () -> Arrays.asList(list);
			writeSheet.setHead(Arrays.asList(Arrays.asList("test")));
			
			ew.write(supplier, writeSheet);
			ew.write(supplier, writeSheet);
		}
	}
}
